package gui.tools;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import tools.MoveTool;

public class MoveToolSetup extends JPanel {

	private MoveTool mMoveTool;
	
	private JTextField mTxtX;
	
	private JTextField mTxtY;
	
	private JButton mBtnOk;
	
	public MoveToolSetup(MoveTool tool) {
		mMoveTool = tool;
		init();
		setEnabled(false);
	}
	
	protected void init() {
		
		GridBagLayout layout = new GridBagLayout();
		GridBagConstraints c = new GridBagConstraints();
		
		setLayout(layout);
		
		c.gridx = 0;
		c.gridy = 0;
		add(new JLabel("dx:"), c);
		
		mTxtX = new JTextField(4);
		c.gridx = 1;
		add(mTxtX, c);
		
		c.gridx = 0;
		c.gridy = 1;
		add(new JLabel("dy:"), c);
		
		mTxtY = new JTextField(4);
		c.gridx = 1;
		add(mTxtY, c);
		
		mBtnOk = new JButton("Ok");
		c.gridx = 0;
		c.gridy = 2;
		c.gridwidth = 2;
		c.anchor = GridBagConstraints.CENTER;
		add(mBtnOk, c);
		
		mBtnOk.addActionListener(mListener);
		
		reset();
	}
	
	public void setEnabled(boolean b) {
		mTxtX.setEnabled(b);
		mTxtY.setEnabled(b);
		mBtnOk.setEnabled(b);
	}
	
	public void reset() {
		mTxtX.setText("0");
		mTxtY.setText("0");
	}
	
	private ActionListener mListener = new ActionListener() {
		
		@Override
		public void actionPerformed(ActionEvent e) {
			int dx = Integer.parseInt(mTxtX.getText());
			int dy = Integer.parseInt(mTxtY.getText());
			
			reset();
			mMoveTool.move(dx, dy);
		}
	};
	
}
